package cn.lh.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

/**
 * @描述: json默认出参设置
 * @版权: Copyright (c): 2024
 * @作者: linhong
 * @版本: 1.0.0
 * @创建时间: 2024/1/3
*/
public class NullCustomConverter {
	 
    /**
     * json处理器
     */
    public static class MyBeanSerializerModifier extends BeanSerializerModifier {
        private NullArrayJsonSerializer array = new NullArrayJsonSerializer();
        private NullStringJsonSerializer string = new NullStringJsonSerializer();
        private NullNumberJsonSerializer number = new NullNumberJsonSerializer();
        private NullBooleanJsonSerializer bool = new NullBooleanJsonSerializer();
        private NullObjectJsonSerializer object = new NullObjectJsonSerializer();
 
        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            /*需要进行添加序列化的类型我们可以在这里进行添加和判断*/
            for (int i = 0; i < beanProperties.size(); i++) {
                BeanPropertyWriter writer = beanProperties.get(i);
                /*判断字段的类型，如果是数组或集合则注册nullSerializer*/
                if (isArrayType(writer)) {
                    /*数组*/
                    writer.assignNullSerializer(this.array);
                } else if (isStringType(writer)) {
                    /*字符串*/
                    writer.assignNullSerializer(this.string);
                } else if (isNumberType(writer)) {
                    /*整形*/
                    writer.assignNullSerializer(this.number);
                } else if (isBooleanType(writer)) {
                    /*布尔*/
                    writer.assignNullSerializer(this.bool);
                } else {
                    writer.assignNullSerializer(this.object);
                }
            }
            return beanProperties;
        }
    }
 
    /**
     * 是否是数组
     */
    private static boolean isArrayType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
    }
 
    /**
     * 是否是String
     */
    private static boolean isStringType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
    }
 
    /**
     * 是否是数值类型
     */
    private static boolean isNumberType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return Number.class.isAssignableFrom(clazz);
    }
 
    /**
     * 是否是boolean
     */
    private static boolean isBooleanType(BeanPropertyWriter writer) {
        Class<?> clazz = writer.getType().getRawClass();
        return clazz.equals(Boolean.class);
    }
 
    /**
     * 处理数组集合类型的null值
     */
    private static class NullArrayJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartArray();
            jsonGenerator.writeEndArray();
        }
    }
 
    /**
     * 处理字符串类型的null值
     */
    private static class NullStringJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString("");
        }
    }
 
    /**
     * 处理数值类型的null值
     */
    private static class NullNumberJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeNumber(0);
        }
    }
 
    /**
     * 处理boolean类型的null值
     */
    private static class NullBooleanJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeBoolean(false);
        }
    }
 
    /**
     * 处理实体对象类型的null值
     */
    private static class NullObjectJsonSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator,
                              SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeEndObject();
        }
    }
}